/* ***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/LGPL 2.1/GPL 2.0

The contents of this file are subject to the Mozilla Public License Version 
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS" basis, 
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.

The Original Code is the Ironsoft Kumquat.

The Initial Developer of the Original Code is
Ironsoft.
Portions created by the Initial Developer are Copyright (C) 2011
the Initial Developer. All Rights Reserved.

Contributor(s):
  Tomas de Priede <tdepriede@ironsoft.com.ar>

Alternatively, the contents of this file may be used under the terms of
either of the GNU General Public License Version 2 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead 
of those above. If you wish to allow use of your version of this file only 
under the terms of either the GPL or the LGPL, and not to allow others to 
use your version of this file under the terms of the MPL, indicate your 
decision by deleting the provisions above and replace them with the notice 
and other provisions required by the GPL or the LGPL. If you do not delete 
the provisions above, a recipient may use your version of this file under
 the terms of any one of the MPL, the GPL or the LGPL

***** END LICENSE BLOCK *****
*/ 

/**
 * File name: LoginPresenter.java
 *
 * Project name: Kumquat
 *
 * Created at: 12/10/2011
 *
 * Author: Tomiito
 */
package ar.com.ironsoft.kumquat.client.presenter;

import ar.com.ironsoft.kumquat.client.events.LoginEvent;
import ar.com.ironsoft.kumquat.client.services.LoginServiceAsync;
import ar.com.ironsoft.kumquat.shared.dto.UserAccountDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Tomiito
 *
 */
public class LoginPresenter implements Presenter {

	private LoginServiceAsync loginService;
	private SimpleEventBus eventBus;
	
	public interface Display {
		String getUsername();
		String getPassword();
		HasClickHandlers getLoginButton();
		Widget asWidget();
	}

	private final Display display;

	public LoginPresenter(LoginServiceAsync loginService,SimpleEventBus eventBus, Display display) {
		this.display = display;
		this.eventBus = eventBus;
		this.loginService = loginService;
	}

	public void bind() {

	    this.display.getLoginButton().addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        doLogin();
	      }
	    });

	  }
	
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		bind();
	}

	public void doLogin(){
		loginService.login(display.getUsername(), display.getPassword(), new AsyncCallback<UserAccountDTO>() {
			
			@Override
			public void onSuccess(UserAccountDTO result) {
				if (result != null)
					eventBus.fireEvent(new LoginEvent(result));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				GWT.log("failure on login rpc call");				
			}
		});
	}
	
}

